package cn.edu.leetcode.其他;

import java.util.ArrayList;

public class 最长公共前缀 {
    public static void main(String[] args) {

        String[] strs = {"fl", "flow", "flight"};

        System.out.println(longestCommonPrefix(strs));
    }

    public static String longestCommonPrefix(String[] strs) {
        ArrayList<char[]> charsList = new ArrayList<>();

        for (String string : strs) {
            charsList.add(string.toCharArray());
        }

        char[] chars = charsList.get(0);
        String string = strs[0];

        for (int i = 0;i < chars.length ; i++) {
            char c = chars[i];
            for (int j = 0; j < charsList.size(); j++) {
                if (i >= charsList.get(j).length || c != charsList.get(j)[i]) {
                    return string.substring(0, i);
                }
            }
        }
        return string;
    }
}
